package com.gitee.openviducn.inspector.rest;

import com.gitee.openviducn.inspector.common.BizException;
import com.gitee.openviducn.inspector.common.ResponseResult;
import com.gitee.openviducn.inspector.domain.vo.request.*;
import com.gitee.openviducn.inspector.domain.vo.response.openvidu.OpenViduVO;
import com.gitee.openviducn.inspector.domain.vo.response.openvidu.SessionVO;
import com.gitee.openviducn.inspector.manager.OpenViduManager;
import io.openvidu.java.client.OpenViduHttpException;
import io.openvidu.java.client.OpenViduJavaClientException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * @Author: https://gitee.com/wesleyOne
 * @Date: 04.19 2020
 */
@Slf4j
@Api(tags = "服务管理接口")
@RequestMapping("/server/manager")
@RestController
public class ServerController  {


    @Resource
    private OpenViduManager openViduManager;

    @ApiOperation("单个OpenVidu详情")
    @PostMapping("/openvidu/detail")
    public ResponseResult<OpenViduVO> getOpenviduDetail(@Validated @RequestBody GetOpenviduDetailRequest openviduDetailRequest) throws ExecutionException, InterruptedException {
        OpenViduVO openViduVO = openViduManager.getOpenViduVO(openviduDetailRequest.getHostname());
        return ResponseResult.success(openViduVO);
    }

    @ApiOperation("单个session详情")
    @PostMapping("/session/detail")
    public ResponseResult<SessionVO> getSessionDetail(@Validated @RequestBody GetSessionDetailRequest sessionDetailRequest) throws ExecutionException, InterruptedException {
        SessionVO sessionVO = openViduManager.getSessionVO(sessionDetailRequest.getHostname(), sessionDetailRequest.getSessionId());
        return ResponseResult.success(sessionVO);
    }

    @ApiOperation("关闭指定session")
    @PostMapping("/session/close")
    public ResponseResult<Void> closeSession(@Validated @RequestBody CloseSessionRequest closeSessionRequest) throws OpenViduJavaClientException, BizException {
        openViduManager.closeSession(closeSessionRequest.getHostname(), closeSessionRequest.getSessionId());
        return ResponseResult.success();
    }

    @ApiOperation("强制断开指定连接connection")
    @PostMapping("/session/force/disconnect")
    public ResponseResult<Void> forceDisconnect(@Validated @RequestBody ForceDisconnectRequest forceDisconnectRequest) throws OpenViduJavaClientException, OpenViduHttpException, BizException {
        openViduManager.forceDisconnect(forceDisconnectRequest.getHostname(), forceDisconnectRequest.getSessionId(), forceDisconnectRequest.getConnectionId());
        return ResponseResult.success();
    }

    @ApiOperation("强制取消发布流")
    @PostMapping("/session/force/unpublish")
    public ResponseResult<Void> forceUnpublish(@Validated @RequestBody ForceUnpublishRequest forceUnpublishRequest) throws OpenViduJavaClientException, BizException {
        openViduManager.forceUnpublish(forceUnpublishRequest.getHostname(), forceUnpublishRequest.getSessionId(), forceUnpublishRequest.getPublishStreamId());
        return ResponseResult.success();
    }

}
